﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using TA.Turbo.Design;



namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all client ID control references.
	/// </summary>
	public abstract class BaseClientId : BaseSpecialControlNoId, IClientId
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseClientId()
		{
			this.ControlId = "";
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Gets or sets the ID of the referenced control, relative to this control reference.
		/// </summary>
		[IDReferenceProperty(typeof(Control)), DefaultValue("")]
		[TypeConverter(typeof(ControlIdConverter<ControlIdLister>))]
		public string ControlId { get; set; }
		/// <summary>
		/// Gets the referenced control. 
		/// </summary>
		/// <remarks>
		/// This readonly property returns the control specified by ControlId.
		/// </remarks>
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public Control Control
		{
			get
			{
				if (_control == null)
				{
					_control = this.FindControlGlobal(ControlId);
					if (_control == null)
						throw new InvalidOperationException(string.Format("Control ID '{0}' not found.", ControlId));
				}
				return _control;
			}
		}
		#endregion
		#region METHODS
		/// <summary>
		/// When implemented, gets the client script used to initialize this client ID reference.
		/// </summary>
		/// <returns></returns>
		public abstract string GetScript();
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		Control _control;
		#endregion
		#endregion
	}
}
